package trab_sd;

import java.util.*;
import java.io.*;

public class FileListNode implements Serializable{
	private String name;
	private UserList owners;
	
	public FileListNode( String name )
	{
		this.name = name;
		owners = new UserList();
	}
	
	public String getName()
	{
		return name;
	}
	
	public void addUser( UserID id )
	{
		owners.add(id);
	}
	
	public UserID getUser()
	{
		return owners.getRandom();
	}
	
	public void removeUser( UserID id )
	{
		UserID tst = owners.getByName(id.getName());
		owners.remove(tst);
	}
	
	public boolean equals( FileListNode node )
	{
		return name.equals( node.getName() )  ;
	}
	
	public Iterator getOwnersIterator()
	{
		return owners.iterator();
	}
	
	public boolean haveUsers() {
		if (!owners.isEmpty())
			return true;
		return false;
	}
	
	public String toString()
	{
		String ret = "";
		Iterator itr = owners.iterator();
		UserID tmp;
		while (itr.hasNext()) {
			tmp = (UserID) itr.next();
			ret = ret + String.format("%-21s %-21s %-10s %-10d\n", name, tmp.getName(), tmp.getIP(), tmp.getPort());
		}
		return ret;
	}
}
